package maze;

import test.Main;

/**
 * @author Denis Toikka
 * 
 */
public class Player {

	/**
	 * Room, where is player located.
	 */
	public Room currentRoom;
	/**
	 * Player's name.
	 */
	public String name = "";
	
	/**
	 * @return
	 * Returns player's current room.
	 */
	public Room getRoom() {
		return this.currentRoom;
	}
	
	/**
	 * @param room
	 * Sets player's current room.
	 */
	private void setRoom(Room room) {
		this.currentRoom = room;
	}
	
	/**
	 * @return
	 * Returns player's name.
	 */
	public String getName() {
		return this.name;
	}
	
	/**
	 * @param s
	 * Sets player's name.
	 */
	public void setName(String s) {
		this.name = s;
	}
	
	/**
	 * @param s - destination("N", "E", "S", "W")
	 * @return
	 * Moves player into another room. 
	 */
	public Player Move(String s) {
		if (getRoom().getDirection(s) != Maze.wall) setRoom(getRoom().getDirection(s));
		else {
			Main.echo("Such a path does not exist");
			return this;
		}
		if (getRoom().isExit) {
			Main.echo(this.name + " found an exit");
			setRoom(Maze.out);
			return this;
		}
		if (getRoom().isPortal) {
			Main.echo(this.name + " teleported from " + getRoom().getName() 
					+ " to " + getRoom().portalTo.getName());
			setRoom(getRoom().portalTo);
		}
		return this;
	}
	
	/**
	 * @param pathList
	 * @return
	 * Validate if final destination exist. 
	 * Then moves player step by step to the final room.
	 */
	public Player Move(String[] pathList) {
		if(getRoom().validatePath(pathList)) {
			for(int i = 0; i < pathList.length; i++) {
				this.Move(pathList[i]);
			}
		}
		else {
			Main.echo("Such a path does not exist");
			return this;
		}
		return this;
	}

	/**
	 * @param maze
	 * Player entering to the maze.
	 */
	public void Enter(Maze maze) {
		this.setRoom(maze.entry);
	}
}
